package com.huluobo.a2202mvvm_im_douyindemo.conversation;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huluobo.a2202mvvm_im_douyindemo.BuildConfig;
import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.huluobo.a2202mvvm_im_douyindemo.adapter.ConversationAdapter;
import com.huluobo.a2202mvvm_im_douyindemo.chat.ChatActivity;
import com.huluobo.a2202mvvm_im_douyindemo.databinding.FragmentConversationBinding;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseMVVMFragment;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;

import java.util.ArrayList;
import java.util.List;

public class ConversationFragment extends BaseMVVMFragment<FragmentConversationBinding, ConversationViewModel> {

    private List<EMConversation> emConversationList = new ArrayList<>();
    private ConversationAdapter conversationAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_conversation;
    }

    @Override
    protected void initData() {
        viewModel.getAllConversations();

        viewModel.getAllConversationsSuccess.observe(this, emConversations -> {
            dataBinding.conversationSrl.setRefreshing(false);
            emConversationList.clear();
            emConversationList.addAll(emConversations);
            conversationAdapter.notifyDataSetChanged();
        });

        viewModel.getAllConversationsFailed.observe(this, s -> {
            dataBinding.conversationSrl.setRefreshing(false);
            Log.i("TagA", "获取会话列表失败:" + s);
        });
    }

    @Override
    protected void initView() {
        dataBinding.conversationRv.setLayoutManager(new LinearLayoutManager(requireContext()));
        conversationAdapter = new ConversationAdapter(emConversationList);
        dataBinding.conversationRv.setAdapter(conversationAdapter);

        conversationAdapter.setOnItemClickListener((adapter, view, position) -> {
            String conversationId = emConversationList.get(position).conversationId();
            Intent intent = new Intent(requireContext(), ChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            int chatType = 0;
            switch (emConversationList.get(position).getLastMessage().getChatType()) {
                case Chat:
                    chatType = 1;
                    break;
                case GroupChat:
                    chatType = 2;
                    break;
            }
            bundle.putInt("chatType", chatType);
            intent.putExtras(bundle);
            startActivity(intent);
        });

        dataBinding.conversationSrl.setOnRefreshListener(() -> {
            viewModel.getAllConversations();
        });

        EMClient.getInstance().chatManager().addMessageListener(messages -> {
            viewModel.getAllConversations();
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.getAllConversations();
    }

    @Override
    protected Class<ConversationViewModel> getViewModel() {
        return ConversationViewModel.class;
    }
}